#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include <QMessageBox>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 固定登录大小窗口
    this->setFixedSize(400, 300);
    // 信号槽监听关闭窗口
    connect(ui->Logout,&QPushButton::clicked, this, &Widget::close);
    // 设置登录窗口无边框
    this->setWindowFlags(Qt::FramelessWindowHint);
    // 监听勾选显示密码
    connect(ui->DisplayPwd, &QCheckBox::clicked, this, [&](bool flag){
        if(flag){
            ui->PassWord->setEchoMode(QLineEdit::Normal);
        }else{
            ui->PassWord->setEchoMode(QLineEdit::Password);
        }
    });
    // 监听记录密码
    connect(ui->MemoryPwd, &QCheckBox::clicked, this, [&](bool flag){
        // 记录状态
        save_pwd = flag;
    });

    // 监听点击登录
    FileOperation *op = FileOperation::getInstance();
    connect(ui->Login,&QPushButton::clicked, this, [&](bool){
        this->manage = new management;
        // 后端校验
        QString data = this->manage->sendModel4(ui->UserName->text(), ui->PassWord->text());
        this->manage->tcp_client->sendData(data);
        connect(this->manage, &management::is_login, this, [&](bool flag){
            qDebug() << flag;
            if(flag){
                // 展示其他界面
                this->manage->show();
                // 收起登录界面
                this->hide();
                // 如果是否记录密码都会保存文件
                if(!save_pwd)
                {
                    this->m_clinetConfig.cl_cfg.password="";
                }else{
                    this->m_clinetConfig.cl_cfg.password=ui->PassWord->text();
                }
                // 获取用户输入的用户名
                this->m_clinetConfig.cl_cfg.username=ui->UserName->text();
                // 将是否记录密码状态保存到文件
                this->m_clinetConfig.cl_cfg.is_save_pwd=save_pwd;
                op->writeConfig(this->m_clinetConfig.cl_cfg);

                // 先获取展示数据
                manage->tcp_client->sendData(manage->sendModel1());
            }else{
                delete this->manage;
                // 弹出保存成功提示
                QMessageBox::information(this,  "result",  "用户名或密码错误!", QMessageBox::Yes, QMessageBox::Yes);
            }
        });

    });
    // 设置窗口标题
    this->setWindowTitle("登录");
    // 右键设置
    m_action = new QAction("设置");
    connect(this->m_action,&QAction::triggered,this, [&](){
       this->m_clinetConfig.exec();
    });
    // 监听修改后的用户名和密码
    connect(&this->m_clinetConfig, &clientconfig::sendUserPwd, this, &Widget::setUserPwd);
    // 尝试获取登录配置
    if(op->readConif(&(m_clinetConfig.cl_cfg)))
    {
        setUserPwd(m_clinetConfig.cl_cfg.username, m_clinetConfig.cl_cfg.password);
        // 从配置文件中获取状态
        save_pwd = m_clinetConfig.cl_cfg.is_save_pwd;
        // 显示是否记录密码
        ui->MemoryPwd->setChecked(save_pwd);
    }
    // 设置背景色
    setBackground();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if(mouse_press)
    {
        move(event->globalPos() - mousePoint);
    }
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    // 如果点击鼠标左键
    if(event->button() == Qt::LeftButton){
        mouse_press = true;
        // 鼠标点击的位置 - 窗口坐标
        mousePoint = event->globalPos() - this->pos();
    }
    if(event->button() == Qt::RightButton)
    {
        // 添加按钮
        this->addAction(m_action);
        // 显示按钮
        setContextMenuPolicy(Qt::ActionsContextMenu);
    }
}

void Widget::mouseReleaseEvent(QMouseEvent*)
{
    mouse_press = false;
}

void Widget::setBackground()
{
    this->setAutoFillBackground(true);
    QPalette p = this->palette();
    // 设置纯背景色
//    p.setColor(QPalette::Window, "#32CD32");
    // 设置背景图
    QPixmap pix(":/images/t1.jpeg");
    p.setBrush(QPalette::Window, QBrush(pix));
    // 设置字体颜色
    p.setColor(QPalette::WindowText, Qt::white);
    this->setPalette(p);



}

void Widget::setUserPwd(QString name, QString pwd)
{
    ui->UserName->setText(name);
    ui->PassWord->setText(pwd);
}
